<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button id="loginBtn">点击创建悬浮窗</button>
    <script>
      // 通用单例函数
      const getSingle = function (fn) {
        let result
        return function () {
          return result || (result = fn.apply(this, arguments))
        }
      }
      const createLoginLayer = function () {
        div = document.createElement('div')
        div.innerHTML = '我是登录悬浮窗'
        div.style.display = 'none'
        document.body.appendChild(div)
        return div
      }

      const createSingleLoginLayer = getSingle(createLoginLayer)

      document.getElementById('loginBtn').onclick = function () {
        let loginLayer = createSingleLoginLayer()
        loginLayer.style.display = 'block'
      }
    </script>
  </body>
</html>
